package com.siant.oauth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPObject;
import com.siant.oauth.base.Result;
import com.siant.oauth.base.UserTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@RestController
@Slf4j
public class OauthLogin {
    /*
    直接登录
     */
    @GetMapping(value = "/loginForWst")
    public Result Login(String userId, HttpServletRequest httpRequest) {

        HttpSession session = httpRequest.getSession();
        UserTest u = new UserTest();
        u.setId(userId);
        u.setName("test");
        u.setPwd("sasasa");
        // 存储sessionId
        session.setAttribute("Usession", u);
        return Result.ok("登录成功");
    }

    /*
    判断seesion
     */
    @GetMapping(value = "/checkLogin")
    public boolean checkLogin(HttpServletRequest httpRequest) {
        HttpSession session = httpRequest.getSession();
        Object obj  =  session.getAttribute("Usession");
        UserTest u = null;
        if(obj instanceof UserTest) {
            u = (UserTest) obj;
            System.out.println(u.getId()+u.getName()+u.getPwd());
            return true;
        }
        return false;

    }


    /*
    直接跳转
    */
    @GetMapping(value = "/toIndex")
    public boolean toIndex(String userId) {
        return true;
    }

    @GetMapping("/index")
    public ModelAndView index(HttpServletRequest httpRequest) {

        HttpSession session = httpRequest.getSession();
        UserTest u = (UserTest) session.getAttribute("Usession");

        ModelAndView mv = new ModelAndView();
        if(u!=null){
            String  ustr = JSON.toJSONString(u);
            // 设置返回的数据
            mv.addObject("user", u);
            //设置视图名
            mv.setViewName("index");
        }else{
            mv.addObject("user", "没登录");
            //设置视图名
            mv.setViewName("index");
        }

        return mv;
    }


}
